<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\BaseController;
use App\Helpers\Utils;
use App\Model\Config;
use App\Service\SmsService;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\RefreshTokenMiddleware;



/**
 * @AutoController()
 * @Middleware(RefreshTokenMiddleware::class)
 */
class SystemController extends BaseController
{
    public function gets()
    {
        $array = [];
         Config::where('group','system')->get()->map(function ($item,$key)use (&$array){
            $array[$item->name] = $item->config;
        });

        return $this->success($array);
    }


    public function sms()
    {
        $post = $this->request->all();

        Config::where('name','sms')->update(['config' => Utils::ArraytoJson($post)]);

        return $this->message('设置成功');
    }

    public function testSms()
    {
        $phone = $this->request->input('phone');

        if (Utils::checkPhone($phone) == false){
            return $this->failed('请输入正确的手机号');
        }

        $result = (new SmsService)->sendSms($phone,'测试短信');

        if ($result == 'ok'){
            return $this->message('发送测试短信成功.请注意查收');
        }

        return $this->failed($result);
    }

    //  上传配置
    public function image()
    {
        $post = $this->request->all();

        Config::where('name','image')->update(['config' => Utils::ArraytoJson($post)]);

        return $this->message('设置成功');
    }

    //  app设置
    public function appSet()
    {
        $post = $this->request->all();

        Config::where('name','app_set')->update(['config' => Utils::ArraytoJson($post)]);

        return $this->message('设置成功');
    }
}
